package com.qixin.modules.rzInvoice.service.impl;

import com.qixin.modules.rzInvoice.entity.EaRzJdpf;
import com.qixin.modules.rzInvoice.entity.EaRzJdpfItems;
import com.qixin.modules.rzInvoice.mapper.EaRzJdpfItemsMapper;
import com.qixin.modules.rzInvoice.mapper.EaRzJdpfMapper;
import com.qixin.modules.rzInvoice.service.IEaRzJdpfService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 机打发票
 * @Author: qixin-boot
 * @Date:   2021-07-08
 * @Version: V1.0
 */
@Service
public class EaRzJdpfServiceImpl extends ServiceImpl<EaRzJdpfMapper, EaRzJdpf> implements IEaRzJdpfService {

	@Autowired
	private EaRzJdpfMapper eaRzJdpfMapper;
	@Autowired
	private EaRzJdpfItemsMapper eaRzJdpfItemsMapper;
	
	@Override
	@Transactional
	public void saveMain(EaRzJdpf eaRzJdpf, List<EaRzJdpfItems> eaRzJdpfItemsList) {
		eaRzJdpfMapper.insert(eaRzJdpf);
		if(eaRzJdpfItemsList!=null && eaRzJdpfItemsList.size()>0) {
			for(EaRzJdpfItems entity:eaRzJdpfItemsList) {
				//外键设置
				entity.setEaRzJdpfId(eaRzJdpf.getId());
				eaRzJdpfItemsMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(EaRzJdpf eaRzJdpf,List<EaRzJdpfItems> eaRzJdpfItemsList) {
		eaRzJdpfMapper.updateById(eaRzJdpf);
		
		//1.先删除子表数据
		eaRzJdpfItemsMapper.deleteByMainId(eaRzJdpf.getId());
		
		//2.子表数据重新插入
		if(eaRzJdpfItemsList!=null && eaRzJdpfItemsList.size()>0) {
			for(EaRzJdpfItems entity:eaRzJdpfItemsList) {
				//外键设置
				entity.setEaRzJdpfId(eaRzJdpf.getId());
				eaRzJdpfItemsMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		eaRzJdpfItemsMapper.deleteByMainId(id);
		eaRzJdpfMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			eaRzJdpfItemsMapper.deleteByMainId(id.toString());
			eaRzJdpfMapper.deleteById(id);
		}
	}
	
}
